#include "pskilldialog.h"
#include "gameanimation.h"
#include "playertable.h"
#include "player.h"
#include<QPainter>

/*

  SKILL BACKGROUND CLASS

*/
SkillBackground::SkillBackground(QGraphicsObject *parent)
    :QGraphicsObject(parent)
{
    setOpacity(0);
}

QRectF SkillBackground::boundingRect() const{
    return QRectF(-pskilldialog::SKILL_WIDTH/2,
                  -pskilldialog::SKILL_HEIGHT/2,
                  pskilldialog::SKILL_WIDTH, pskilldialog::SKILL_HEIGHT);
}

void SkillBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
}

/*

  SKILL TEXT INFO CLASS

*/
SkillTextInfo::SkillTextInfo(QGraphicsObject *parent, QString content)
    :QGraphicsObject(parent),_content(content)
{
    setOpacity(0);
}

QRectF SkillTextInfo::boundingRect() const{
    return QRectF(-pskilldialog::SKILL_WIDTH/2+10+SkillOwner::SKILL_OWNER_WIDTH,
                  pskilldialog::SKILL_WIDTH/2-10-SkillOwner::SKILL_OWNER_HEIGHT,
                  SKILL_TEXT_WIDTH, SkillOwner::SKILL_OWNER_HEIGHT);
}

void SkillTextInfo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::red);
    painter->drawText(boundingRect(), Qt::AlignLeft, " Text area\n Skill description\n "+_content);
}

/*

  SKILL OWNER CLASS

*/


SkillOwner::SkillOwner(QGraphicsObject *parent)
    :QGraphicsObject(parent)
{
    setOpacity(0);
}

QRectF SkillOwner::boundingRect() const{
    return QRectF(-pskilldialog::SKILL_WIDTH/2+10, pskilldialog::SKILL_WIDTH/2-10-SKILL_OWNER_HEIGHT,
                  SKILL_OWNER_WIDTH, SKILL_OWNER_HEIGHT);
}

void SkillOwner::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::blue);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Player head");
}

/*

  SKILL OWNER BIG CLASS

*/


SkillOwnerBig::SkillOwnerBig(QGraphicsObject *parent, const QPixmap &pix)
    :QGraphicsObject(parent), _pix(pix)
{
    setOpacity(0);
}

QRectF SkillOwnerBig::boundingRect() const{
    return QRectF(-_pix.width()/2, -_pix.height()/2, _pix.width(), _pix.height());
}

void SkillOwnerBig::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
}

/*

  SKILL DIALOG CLASS


  */

pskilldialog::pskilldialog(QGraphicsObject* parent)
    :QGraphicsObject(parent)
{

    diaBlock = new InputBlocker(parent);
    diaBlock->setZValue(diaBlock->zValue()+1);
    this->setZValue(zValue()+2);
    this->setVisible(false);


}


QRectF pskilldialog::boundingRect() const{
    return QRectF(-SKILL_WIDTH/2, -SKILL_HEIGHT/2, SKILL_WIDTH, SKILL_HEIGHT);
}

void pskilldialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

}

void pskilldialog::createAnimation(){
    enterAnim = new GameSeqAnimation(this);

    GameParAnimation* enterFore = new GameParAnimation(enterAnim);
    enterFore->addFade(diaBlock, 0, 0.5, 300);

    GameParAnimation* enterBack = new GameParAnimation(enterAnim);
    enterBack->addFade(back, 0, 1, 200);

    GameParAnimation* enterOwner = new GameParAnimation(enterAnim);
    enterOwner->addFade(ownerBig, 0, 1, 300);
    enterOwner->addMove(ownerBig, QPointF(20,0), QPointF(0,0), 300);

    GameParAnimation* enterText = new GameParAnimation(enterAnim);
    enterText->addFade(owner, 0, 1, 200);
    enterText->addFade(text, 0, 1, 200);

    enterAnim->addAnimation(enterFore);
    enterAnim->addAnimation(enterBack);
    enterAnim->addAnimation(enterOwner);
    enterAnim->addAnimation(enterText);

    connect(enterAnim, SIGNAL(finished()), this, SLOT(skill()));
}

void pskilldialog::prepare(){

    this->setVisible(true);
    diaBlock->setVisible(true);
    back->setOpacity(0);
    owner->setOpacity(0);
    ownerBig->setOpacity(0);
    text->setOpacity(0);
}

void pskilldialog::enter(){
    prepare();

    enterAnim->start();
}


void pskilldialog::skill(){
    useSkill();
}


void pskilldialog::leave(){
    //exit animation
    GameParAnimation* exit = new GameParAnimation(this);
    exit->addFade(this, 1, 0, 300);
    exit->addFade(diaBlock, 0.5, 0, 300);
    exit->start(QAbstractAnimation::DeleteWhenStopped);
    connect(exit, SIGNAL(finished()), this, SLOT(finished()));
}

void pskilldialog::finished(){
    //stuff after anim finished
    diaBlock->setVisible(false);
    this->setVisible(false);
    this->setOpacity(1);
}



/*

  PLAYER TAO CLASS

*/

PlayerTao::PlayerTao(QGraphicsObject *parent,const QPixmap &pix)
    :QGraphicsObject(parent), _taoPix(pix)
{
    //this->setOpacity(0);
    _width = _taoPix.width();
    _height = _taoPix.height();
    setAcceptHoverEvents( true );
}

QRectF PlayerTao::boundingRect() const
{
    return QRectF( -_width/2 * this->scale() - 10, -_height/2 * this->scale() - 10,
                   _width*this->scale() + 20, _height*this->scale() + 20 );
}

void PlayerTao::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    qreal myscale = 2;
    bool over = option->state & QStyle::State_MouseOver;
    if( over )
    {
        myscale = 1.98;
    }
    painter->drawPixmap( -_width * myscale / 2, - _height * myscale / 2,
                         _width * myscale, _height * myscale, _taoPix );

}

void PlayerTao::mousePressEvent(QGraphicsSceneMouseEvent *event){
    emit pressed();
    update();
}

void PlayerTao::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    update();
}

/*

  YELLOW PLAYER SKILL CLASS

*/

YellowSkill::YellowSkill(QGraphicsObject *parent, player* pl,const QPixmap &pix)
    :pskilldialog(parent)
{
    _Player = pl;
    //components instantiate
    back = new SkillBackground(this);
    ownerBig = new SkillOwnerBig(this, pix);
    owner = new SkillOwner(this);
    text = new SkillTextInfo(this, QString("Choose one color"));
    createAnimation();

}

void YellowSkill::TaoAnimation(){
    yellowTao->setOpacity(0);
    greenTao->setOpacity(0);
    blueTao->setOpacity(0);
    redTao->setOpacity(0);
    blackTao->setOpacity(0);
    GameSeqAnimation* taoAnim = new GameSeqAnimation(this);

    GameParAnimation* yellowAnim = new GameParAnimation(taoAnim);
    yellowAnim->addFade(yellowTao, 0.2, 1, 300);

    GameParAnimation* greenAnim = new GameParAnimation(taoAnim);
    greenAnim->addFade(greenTao, 0.2, 1, 300);

    GameParAnimation* blueAnim = new GameParAnimation(taoAnim);
    blueAnim->addFade(blueTao, 0.2, 1, 300);

    GameParAnimation* redAnim = new GameParAnimation(taoAnim);
    redAnim->addFade(redTao, 0.2, 1, 300);

    GameParAnimation* blackAnim = new GameParAnimation(taoAnim);
    blackAnim->addFade(blackTao, 0.2, 1, 300);

    taoAnim->addAnimation(yellowAnim);
    taoAnim->addAnimation(greenAnim);
    taoAnim->addAnimation(blueAnim);
    taoAnim->addAnimation(redAnim);
    taoAnim->addAnimation(blackAnim);

    taoAnim->start();
    connect(taoAnim, SIGNAL(finished()), this, SLOT(connectTao()));
}


void YellowSkill::useSkill(){
    redTao = new PlayerTao(this,QPixmap(":/Tao/images/shu.png"));
    redTao->setPos(-74,100);

    yellowTao = new PlayerTao(this,QPixmap(":/Tao/images/god.png"));
    yellowTao->setPos(0,-127);

    blueTao = new PlayerTao(this,QPixmap(":/Tao/images/wei.png"));
    blueTao->setPos(74,100);

    greenTao = new PlayerTao(this,QPixmap(":/Tao/images/wu.png"));
    greenTao->setPos(120,-40);

    blackTao = new PlayerTao(this,QPixmap(":/Tao/images/qun.png"));
    blackTao->setPos(-120,-40);

    TaoAnimation();

}

void YellowSkill::addRedTao(){
    qDebug("Red Tao has added");
    _Player->resistChange0(true);
    leaveTao();
}
void YellowSkill::addYellowTao(){
    qDebug("Yellow Tao has added");
    _Player->resistChange1(true);
    leaveTao();
}
void YellowSkill::addBlueTao(){
    qDebug("Blue Tao has added");
    _Player->resistChange2(true);
    leaveTao();
}
void YellowSkill::addGreenTao(){
    qDebug("Green Tao has added");
    _Player->resistChange3(true);
    leaveTao();
}
void YellowSkill::addBlackTao(){
    qDebug("Black Tao has added");
    _Player->resistChange4(true);
    leaveTao();
}

void YellowSkill::connectTao(){
    connect(redTao, SIGNAL(pressed()), this, SLOT(addRedTao()));
    connect(yellowTao, SIGNAL(pressed()), this, SLOT(addYellowTao()));
    connect(blueTao, SIGNAL(pressed()), this, SLOT(addBlueTao()));
    connect(greenTao, SIGNAL(pressed()), this, SLOT(addGreenTao()));
    connect(blackTao, SIGNAL(pressed()), this, SLOT(addBlackTao()));
}

void YellowSkill::leaveTao(){
    GameParAnimation* exitTao = new GameParAnimation(this);
    exitTao->addFade(blackTao, 1, 0, 300);
    exitTao->addFade(redTao, 1, 0, 300);
    exitTao->addFade(blueTao, 1, 0, 300);
    exitTao->addFade(greenTao, 1, 0, 300);
    exitTao->addFade(yellowTao, 1, 0, 300);
    exitTao->start(QAbstractAnimation::DeleteWhenStopped);
    connect(exitTao, SIGNAL(finished()), this, SLOT(finished()));
}


